package com.mingqijia.gassafety.shared.handler;

import com.gap.basic.base.CommonResponse;
import com.gap.basic.exception.BizException;
import com.mingqijia.gassafety.shared.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.Optional;

/**
 * 系统异常统一拦截处理类
 * @author Damon S.
 */
@Slf4j
@RestControllerAdvice
public final class SecurityExceptionHandler {

    @ResponseBody
    @ExceptionHandler(AuthenticationException.class)
    public CommonResponse<Void> notExpected(AuthenticationException failed) {
        if (failed instanceof GlobalAuthenticationException &&
                ((GlobalAuthenticationException) failed).isLogStack()) {
            log.error("安全认证异常", failed);
        }
        return CommonResponse.failure(failed.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(AccessDeniedException.class)
    public CommonResponse<Void> notExpected(AccessDeniedException failed) {
        return CommonResponse.failure(failed.getMessage());
    }
}